package com.lee.demo.ui.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.lee.demo.R;

import java.util.ArrayList;
import java.util.List;

import cn.lee.cplibrary.util.ToastUtil;
import cn.lee.cplibrary.widget.anim.FreeTextView;

/**
 * 实现屏幕底部向上滑动效果（使用BottomSheet）,参考：https://www.jianshu.com/p/ae866a43c8e3
 * 1.引入依赖包 implementation "com.google.android.material:material:1.0.0"
 * 2.根布局必须是CoordinatorLayout
 * 3.底部页面必须具有可滑动特性，常规一般外层嵌套NestedScrollView，如本身View可滑动，例如RecyclerView则可不用外层嵌套NestedScrollView
 * 4.滑动页面可为任意布局，必须加属性app:layout_behavior="@string/bottom_sheet_behavior"
 * <p>
 * 类似效果实现：https://blog.csdn.net/c_cayujie/article/details/70548332，https://github.com/yingLanNull/ScrollLayout
 */
public class BottomScrollActivity extends AppCompatActivity {
    //    private NestedScrollView nestedScrollView;
    private BottomSheetBehavior behavior;
    //    private WebView webView;
    private Button btn_expand;
    private Button btn_collapsed;
    private Button btn_change;
    private FreeTextView tv;
    private RecyclerView recyclerView;
    //    private ImageView iv_top;
    private List<String> totalList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bottom_scroll);

//        nestedScrollView = (NestedScrollView) findViewById(R.id.nested_scroll_view);
//        webView = (WebView) findViewById(R.id.web_view);
//        webView.loadUrl("https://www.dazhuanlan.com/2019/12/10/5dee9b9331e45/");
        btn_expand = (Button) findViewById(R.id.btn_expand);
        btn_collapsed = (Button) findViewById(R.id.btn_collapsed);
        btn_change = (Button) findViewById(R.id.btn_change);
        //测试滑动时FreeTextView显示
        tv = (FreeTextView) findViewById(R.id.tv);
//        iv_top = findViewById(R.id.iv_top);
        recyclerView = findViewById(R.id.recyclerView);
        initRecyclerView();
//        behavior = BottomSheetBehavior.from(nestedScrollView);

        behavior = BottomSheetBehavior.from(recyclerView);
        behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
        behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                //这里是bottomSheet 状态的改变
                switch (newState) {
                    //展开状态
                    case BottomSheetBehavior.STATE_EXPANDED:
//                        iv_top.setImageDrawable(getDrawable(R.drawable.shaixuan_down));
                        break;
                    //拖动状态
                    case BottomSheetBehavior.STATE_DRAGGING:
//                        iv_top.setImageDrawable(getDrawable(R.drawable.bg_xiadan));
                        break;
                    //折叠状态
                    case BottomSheetBehavior.STATE_COLLAPSED:
//                        iv_top.setImageDrawable(getDrawable(R.drawable.shaixuan_up));
                        break;
                    default:
                        break;
                }

            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {
                //这里是拖拽中的回调，根据slideOffset可以做一些动画
            }
        });

        btn_expand.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
            }
        });

        btn_collapsed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
            }
        });

        btn_change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                recyclerView.setBackgroundColor(Color.parseColor("#F7D0D6"));
            }
        });

        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!tv.isDrag()) {
                    Toast.makeText(BottomScrollActivity.this, "点击了FreeTextView", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    private void initRecyclerView() {
        for (int i = 0; i < 20; i++) {
            totalList.add("卖报的小行家" + i);
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new SelectAdapter(this, totalList));
    }

    public class SelectAdapter extends RecyclerView.Adapter<SelectAdapter.ViewHolder> {
        private Context context;
        private List<String> totalList;

        public SelectAdapter(Context context, List<String> list) {
            this.context = context;
            this.totalList = list;
        }

        @NonNull
        @Override
        public SelectAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            //若root参数使用parent 则只会显示第一行
            View view = LayoutInflater.from(context).inflate(R.layout.item_rv_demo, null, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull SelectAdapter.ViewHolder holder, final int position) {
            holder.btn_select.setText(totalList.get(position));
            holder.btn_select.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ToastUtil.showToast(context, "点击了position=" + position);
                }
            });
        }

        @Override
        public int getItemCount() {
            return totalList.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {

            private Button btn_select;

            public ViewHolder(View itemView) {
                super(itemView);
                btn_select = itemView.findViewById(R.id.btn_select);

            }
        }
    }

}
